package cn.zh.npro.Util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.Utils;

import cn.zh.npro.R;


/**
 * Created by WYN on 2018/3/30 0030.
 */

public class MyMarkerView extends MarkerView {

    private TextView tvContent;
     int wchart=0,x=0,y=0;
    int width =0,dwith=0;
    int height = 0,dhigu=0;
    int ea=0;
    public static final int ARROW_SIZE = 40; // 箭头的大小
    private static final int STOKE_WIDTH =3;
    private static final float CIRCLE_OFFSET = 10;
    public MyMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int with=outMetrics.widthPixels;
        ea=DensityUtil.dp2px(context,20);
        dwith=DensityUtil.dp2px(context,60);
        dhigu=DensityUtil.dp2px(context,30);
        System.out.println("------->dp=="+ea+"===="+with);
        wchart=with-ea;
        tvContent = (TextView) findViewById(R.id.tvContent);
        }

    // callbacks everytime the MarkerView is redrawn, can be used to update the
    // content (user-interface)

    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;

            tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
         } else {

            tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true));
           }
         }

    @Override
    public int getXOffset(float posX) {
        width = getWidth();
        if (posX > wchart - width) {//如果超过右边界，则向左偏移markerView的宽度
           x=-width;
            return  -width;
        } else {//默认情况，不偏移（因为是点是在左上角）

            if (posX > width / 2) {//如果大于markerView的一半，说明箭头在中间，所以向右偏移一半宽度
               x=-(width/2);
                return -(width/2);
            }else {
                x=0;
                return  0;
            }
         }
//        return -(getMeasuredWidth() / 2);
       }


    @Override
    public int getYOffset(float posY) {
        height = getHeight();
        if (posY <= height + ARROW_SIZE) {// 如果点y坐标小于markerView的高度，如果不处理会超出上边界，处理了之后这时候箭头是向上的，我们需要把图标下移一个箭头的大小
            y=ARROW_SIZE;
            return ARROW_SIZE;
        } else {
            y=-height - ARROW_SIZE - STOKE_WIDTH;
            return -height - ARROW_SIZE - STOKE_WIDTH;
        }
//        return  -getMeasuredHeight();
       }


    @Override
    public void draw(Canvas canvas, float posX, float posY) {
        super.draw(canvas, posX, posY);
        Paint paint = new Paint();//绘制边框的画笔
        paint.setStrokeWidth(STOKE_WIDTH);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setColor(getResources().getColor(R.color.colorPrimary));

        Paint whitePaint = new Paint();//绘制底色白色的画笔
        whitePaint.setStyle(Paint.Style.FILL);
        whitePaint.setColor(getResources().getColor(R.color.labcor));

        float width = getWidth();
        float height = getHeight();

        int saveId = canvas.save();
        Path path = new Path();

        if (posY < height + ARROW_SIZE) {//处理超过上边界
            path = new Path();
            path.moveTo(0, 0);
            if (posX > wchart - width) {//超过右边界
                path.lineTo(width - ARROW_SIZE, 0);
                path.lineTo(width, -ARROW_SIZE + CIRCLE_OFFSET);
                path.lineTo(width, 0);
            }else {
                if (posX > width / 2) {//在图表中间
                    path.lineTo(width / 2 - ARROW_SIZE / 2, 0);
                    path.lineTo(width / 2, -ARROW_SIZE + CIRCLE_OFFSET);
                    path.lineTo(width / 2 + ARROW_SIZE / 2, 0);
                } else {//超过左边界
                    path.lineTo(0, -ARROW_SIZE + CIRCLE_OFFSET);
                    path.lineTo(0 + ARROW_SIZE, 0);
                }
             }
            path.lineTo(0 + width, 0);
            path.lineTo(0 + width, 0 + height);
            path.lineTo(0, 0 + height);
            path.lineTo(0, 0);
            path.offset(posX + x, posY +y);
          }else {//没有超过上边界
            path = new Path();
            path.moveTo(0, 0);
            path.lineTo(0 + width, 0);
            path.lineTo(0 + width, 0 + height);
            if (posX > wchart - width) {
                path.lineTo(width, height + ARROW_SIZE - CIRCLE_OFFSET);
                path.lineTo(width - ARROW_SIZE, 0 + height);
                path.lineTo(0, 0 + height);
            } else {
                if (posX > width / 2) {
                    path.lineTo(width / 2 + ARROW_SIZE / 2, 0 + height);
                    path.lineTo(width / 2, height + ARROW_SIZE - CIRCLE_OFFSET);
                    path.lineTo(width / 2 - ARROW_SIZE / 2, 0 + height);
                    path.lineTo(0, 0 + height);
                } else {
                    path.lineTo(0 + ARROW_SIZE, 0 + height);
                    path.lineTo(0, height + ARROW_SIZE - CIRCLE_OFFSET);
                    path.lineTo(0, 0 + height);
                }
            }
            path.lineTo(0, 0);
            path.offset(posX + x, posY +y);
           }
        // translate to the correct position and draw
        canvas.drawPath(path, whitePaint);
        canvas.drawPath(path, paint);
        canvas.translate(posX + x, posY + y);
        draw(canvas);
        canvas.restoreToCount(saveId);
        }
     }
